<?php

include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/mblog/inc.php");

error_reporting(E_ERROR | E_PARSE);

//error_reporting(E_ALL);
//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	cgi::both($g_cgival['t'], "t", "json");
		
	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	cgi::both($g_cgival['content'], "content", "");
	$g_cgival['content'] = string::un_html($g_cgival['content']);

	cgi::both($g_cgival['laji_uid'], "laji_uid", "");
	
	cgi::both($g_cgival['class_id'], "class_id", 0);
	$g_cgival['class_id'] = intval($g_cgival['class_id']);
	
	cgi::both($g_cgival['add_black'], "add_black", 0);
	$g_cgival['add_black'] = intval($g_cgival['add_black']);
	
	
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	$token = $_SESSION['token']['access_token'];
	//$token = "2.005hzbtBooBFMC8eb8fea9a8ZZh1aD";
	$content = $g_cgival['content'];
	$ip = IP::get_client_ip();
	$type = 3;
	$class_id   = $g_cgival['class_id'];
	$laji_uid = $g_cgival['laji_uid'];
	
	$url = "http://weibo.com/{$laji_uid}";
	if(!$laji_uid)
	{
		throw new Exception("选择不能为空～～");
		
	}
	
	$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
	$current_uid = $c->get_uid();
	$current_uid = $current_uid["uid"];
	
	$mod_get_mblog_by_token = new mod_get_mblog_by_token();
	$db_fanlaji_jubao_uid = new db_fanlaji_jubao_uid();
	$db_fanlaji_laji_uid = new db_fanlaji_laji_uid();
	
	$jubao_success_uid = array();
	$have_send_today = array();
	foreach($laji_uid as $key => $value)
	{
		list($uid,$nickname) = explode("|||",$value);
		
		$insert_res = $db_fanlaji_jubao_uid->insert_fanlaji_jubao_uid($current_uid,"",$uid,$nickname);
		
		if($g_cgival['add_black'])
		{
			$black_res = $mod_get_mblog_by_token->api_add_blocking_by_token($token,$uid);
		}
			
			
		$db_fanlaji_laji_uid->insert_fanlaji_laji_uid($uid,$nickname);
		
		if(!$insert_res)
		{
			$have_send_today[] = $nickname;
		}
		else
		{
			$res = $mod_get_mblog_by_token->api_add_spam_by_token($token,$url,$content,$ip,$type,$uid,$class_id);
			
			
			
		}
		
		
		
		/*if($res["data"]["errno"] ==1)
		{
			$jubao_success_uid[] = $value;
			
		}
		*/
	}
	
	if($have_send_today)
	{
		$nicknames = join(",",$have_send_today);
		throw new Exception("{$nicknames} 这些用户你今天已经举报过了，由于接口调用次数限制，我们就不重复发了。。其他用户已经举报成功");
		
	}
	
	
	/*$mod_get_mblog_by_token = new mod_get_mblog_by_token();
	$black_list = $mod_get_mblog_by_token->api_get_blocking_by_token($token,1,20);
	*/
	
}
try {
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = "";
	
	
} 
catch (Exception $e) 
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
	
}
show_pro();

exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}
?>
